package com.jane.gzzb;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ListView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jane.adapter.hostheadlinesadapter.PlatformAdapter;
import com.jane.bean.JsonData;
import com.jane.bean.OfflineBean;
import com.jane.config.NetConfig;
import com.jane.utils.Utils;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

public class PlatformActivity extends AppCompatActivity  {
    private ListView mListView;
    private PlatformAdapter mAdapter;
    private ArrayList<OfflineBean> mData;
    private String path;
    private int mPagerIndex;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_off_line);
        initView();
        initData();
    }

    private void initView() {
        mListView= (ListView) findViewById(R.id.listview_offline);
    }
    private void initData() {
        mData=new ArrayList<>();
        mAdapter=new PlatformAdapter(this,mData);
        mListView.setAdapter(mAdapter);
        path = NetConfig.OFFLINE_PATH;
        String jsonString= Utils.getJsonString(path);
        if (!TextUtils.isEmpty(jsonString)){
            parseData(jsonString);
        }else {
            if (!Utils.isNetwork(this)){
                return;
            }else {
                netLoadData();
            }
        }
    }

    private void parseData(String jsonString) {
        mData.clear();
        List<OfflineBean> bean=new Gson().fromJson(jsonString,new TypeToken<List<OfflineBean>>(){}.getType());
        mData.addAll(bean);
        mAdapter.notifyDataSetChanged();
    }

    private void netLoadData() {

        x.http().get(new RequestParams(path), new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                if (mPagerIndex==0){
                    JsonData jsonData=new JsonData();
                    jsonData.setJsonString(result);
                    jsonData.setPath(path);
                    Utils.saveJsonString(jsonData);
                }
                if (!TextUtils.isEmpty(result)){
                    parseData(result);
                }
            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    public void onClick(View view) {
        if (view.getId()==R.id.iv_back){
            finish();
        }
    }


}
